package com.accesshollywood.grfxOrdering;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;

import com.accesshollywood.SQL.GFXSQL;
import com.accesshollywood.grfxOrdering.Dialogs.AbstractCustomDialog;
import com.accesshollywood.indexables.Destination;
import com.accesshollywood.indexables.GraphicType;
import com.accesshollywood.indexables.ShowName;
import com.accesshollywood.indexables.User;

public class ComboBoxPlus extends JComboBox {
	String newEntryLabel = "";

	public ComboBoxPlus() {
		super();
	}

	public ComboBoxPlus(final String newEntryLabel, String newDialogClassName) {
		super();
		this.newEntryLabel = newEntryLabel;
		addActionListener(new CustomDialogActionListener(newDialogClassName));
	}

	public ComboBoxPlus(final String newEntryLabel, String tableName, String fieldName) {
		super();
		this.newEntryLabel = newEntryLabel;
		addActionListener(new GenericActionListener(tableName, fieldName));
	}

	@Override
	public void setModel(ComboBoxModel model) {
		((DefaultComboBoxModel) model).removeElement(newEntryLabel);
		((DefaultComboBoxModel) model).addElement(newEntryLabel);
		super.setModel(model);
	}

	public class CustomDialogActionListener implements ActionListener {
		String customDialogClassName;
		AbstractCustomDialog customDialog = null;

		public CustomDialogActionListener(String customDialogClassName) {
			this.customDialogClassName = customDialogClassName;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			JComboBox cb = (JComboBox) e.getSource();
			if(cb.getSelectedItem() instanceof String){
				String itemText = (String) cb.getSelectedItem();
				int itemIndex = cb.getSelectedIndex();
	
				if (itemText.equals(newEntryLabel)) {
					Class<?> newDialogClass;
					try {
						newDialogClass = Class.forName(customDialogClassName);
						customDialog = (AbstractCustomDialog) newDialogClass.newInstance();
						//Trace.info(itemIndex);
						cb.setSelectedIndex(cb.getModel().getSize()-2);
						//Trace.info((cb.getModel().getSize()-2) + " " + itemIndex);
					} catch (ClassNotFoundException e1) {
						e1.printStackTrace();
					}
					catch (InstantiationException e1) {
						e1.printStackTrace();
					} catch (IllegalAccessException e1) {
						e1.printStackTrace();
					}
				}
			}
		}
	}
	
	public class ComboBoxPlusShowNameRenderer extends DefaultListCellRenderer{
		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
			if(value instanceof ShowName){
				ShowName currShowName = (ShowName)value;
				return new JLabel(currShowName.name);
			}else{
				return new JLabel((String)value);
			}
		}
	}
	
	public class ComboBoxPlusGraphicTypeRenderer extends DefaultListCellRenderer{
		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
			if(value instanceof GraphicType){
				GraphicType currGraphicType = (GraphicType)value;
				return new JLabel(currGraphicType.getName());
			}else{
				return new JLabel((String)value);
			}
		}
	}
	
	public class ComboBoxPlusUserRenderer extends DefaultListCellRenderer{
		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
			if(value instanceof User){
				User currUser = (User)value;
				return new JLabel(currUser.getFullName());
			}else{
				return new JLabel((String)value);
			}
		}
	}
	
	public class ComboBoxPlusDestinationRenderer extends DefaultListCellRenderer{
		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
			if(value instanceof Destination){
				Destination currDestination = (Destination)value;
				return new JLabel(currDestination.getName());
			}else{
				return new JLabel((String)value);
			}
		}
	}

	public class GenericActionListener implements ActionListener {
		public String tableName;
		public String fieldName;

		GenericActionListener(String tableName, String fieldName) {
			this.tableName = tableName;
			this.fieldName = fieldName;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			JComboBox cb = (JComboBox) e.getSource();
			String itemText = (String) cb.getSelectedItem();
			int itemIndex = cb.getSelectedIndex();

			if (itemText.equals(newEntryLabel)) {
				String str = JOptionPane.showInputDialog(null, "New Item...");

				if (str != null) {
					((DefaultComboBoxModel) cb.getModel()).insertElementAt(str, itemIndex);
					cb.setSelectedIndex(itemIndex);
					if (tableName != null && fieldName != null) {
						GFXSQL.addString(tableName, fieldName, str);
					}
				}
			}
		}
	}
}